<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/5
 * Time: 14:21
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Hot extends Base {
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('hot')->count();// 获取总条数
            $lists = Db::name('hot')
                ->page($Nowpage,$limits)
                ->order('hot_sort desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'hot_sort'=>input('value'),
        ];
        $res = Db::name('hot')->where('hot_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function del(){
        $id = input('id');
        $res = Db::table('up_hot')->where('hot_id', $id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    public function add(){
        if(Request::instance()->isPost()){
            $banner_title = trim(input('banner_title'), ' ');
            $banner_sort = input('banner_sort');
            $data = [
                'hot_name' => $banner_title,
                'create_time' => date('Y-m-d H:i:s'),
                'hot_sort' =>$banner_sort
            ];
            $res = Db::table('up_hot')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }

        return $this->fetch();
    }
}